package com.braveman.im.ui.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.braveman.im.R;
import com.braveman.im.bean.AddFriendsAndGroupBean;
import com.braveman.im.bean.DataListBean;
import com.braveman.im.bean.HomeOpreratorBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.activity.message.GroupInfoActivity;
import com.braveman.im.ui.activity.message.QrCodeActivity;
import com.braveman.im.ui.adapter.AddFriendsAdapter;
import com.braveman.im.ui.adapter.AddGroupAdapter;
import com.braveman.im.ui.adapter.AddPlateformAdapter;
import com.braveman.im.utils.UtilsStyle;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.zxing.integration.android.IntentIntegrator;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.widget.TitleBarView;
import com.ht.baselibs.utils.ActivityUtils;
import com.vondear.rxtools.RxBarTool;

import java.util.ArrayList;
import java.util.List;

import ezy.ui.layout.LoadingLayout;
import retrofit2.Call;
import retrofit2.Response;

public class SystemNotifyActivity extends BaseActivity {
    private RelativeLayout mRlUnderway;
    private RelativeLayout mRlDaishenhe;
    private RelativeLayout mRlActivityFlat;
    private TextView mTvTitle1;
    private TextView mIvShenHeZhong;
    private RelativeLayout mRlOver;
    private TextView mTvTitle2;
    private TextView mIvYiShenHe;
    private TextView mIvShenHe2;


    private LinearLayout mLlActivityQrCode;
    private TextView mTvShowLine;
    private LinearLayout mLlActivityPhoneFriends;
    private TextView mTvActivityAddFriendsHint;
    private RecyclerView mLvActivityListMember;
    private List<DataListBean> groupMemberBeanList = new ArrayList<>();
    private LoadingLayout loadingLayout;
    private SwipeRefreshLayout mRefreshLayout;

    private AddFriendsAdapter addFriendsAdapter;
    private AddGroupAdapter addGroupAdapter;
    private AddPlateformAdapter addPlateformAdapter;
    private List<HomeOpreratorBean> list = new ArrayList<>();
    private int mRefreshType = 0;


    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {

    }
    
    @Override
    protected int initContentView() {
        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_system_notify;
    }

    @Override
    protected void initChildView() {
        mRlUnderway = (RelativeLayout) findViewById(R.id.rl_underway);
        mRlDaishenhe = (RelativeLayout) findViewById(R.id.rl_daishenhe);
        mTvTitle1 = (TextView) findViewById(R.id.tv_title1);
        mIvShenHeZhong = (TextView) findViewById(R.id.iv_shen_he_zhong);
        mRlOver = (RelativeLayout) findViewById(R.id.rl_over);
        mTvTitle2 = (TextView) findViewById(R.id.tv_title2);
        mIvYiShenHe = (TextView) findViewById(R.id.iv_yi_shen_he);

        mLlActivityQrCode = (LinearLayout) findViewById(R.id.ll_activity_qr_code);
        mTvShowLine = (TextView) findViewById(R.id.tv_show_line);
        mLlActivityPhoneFriends = (LinearLayout) findViewById(R.id.ll_activity_phone_friends);
        mTvActivityAddFriendsHint = (TextView) findViewById(R.id.tv_activity_add_friends_hint);
        mRlActivityFlat = (RelativeLayout) findViewById(R.id.rl_activity_flat);
        mIvShenHe2 = (TextView) findViewById(R.id.iv_yi_shen_he2);

        loadingLayout = (LoadingLayout) findViewById(R.id.loading_layout);
        mLvActivityListMember = (RecyclerView) findViewById(R.id.lv_Activity_list_member);
        mLvActivityListMember.setLayoutManager(new LinearLayoutManager(this));
        mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_layout);

        addFriendsAdapter = new AddFriendsAdapter(R.layout.list_add_friends_item, groupMemberBeanList);
        addGroupAdapter = new AddGroupAdapter(R.layout.list_add_friends_item, groupMemberBeanList);
        addPlateformAdapter = new AddPlateformAdapter(R.layout.list_add_friends_item, groupMemberBeanList);
        mLvActivityListMember.setAdapter(addFriendsAdapter);
    }

    @Override
    protected void initChildData() {
        mRlUnderway.setSelected(true);
        doNetwork(0);
    }

    @Override
    protected void setChildViewListener() {
        mRlUnderway.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                reSelect();
                mRlUnderway.setSelected(true);
                mIvShenHeZhong.setVisibility(View.VISIBLE);
                mIvYiShenHe.setVisibility(View.GONE);
                mLlActivityPhoneFriends.setVisibility(View.VISIBLE);
                mTvActivityAddFriendsHint.setText("可能感兴趣的人");
                mTvShowLine.setVisibility(View.VISIBLE);
                mIvShenHe2.setVisibility(View.GONE);
                mRefreshType = 0;
                doNetwork(mRefreshType);
            }
        });
        mRlOver.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                reSelect();
                mRlOver.setSelected(true);
                mIvYiShenHe.setVisibility(View.VISIBLE);
                mIvShenHeZhong.setVisibility(View.GONE);
                mLlActivityPhoneFriends.setVisibility(View.GONE);
                mTvActivityAddFriendsHint.setText("可能感兴趣的群");
                mTvShowLine.setVisibility(View.GONE);
                mIvShenHe2.setVisibility(View.GONE);
                mRefreshType = 1;
                doNetwork(mRefreshType);

            }
        });

        mRlActivityFlat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reSelect();
                mRlActivityFlat.setSelected(true);
                mIvYiShenHe.setVisibility(View.GONE);
                mIvShenHeZhong.setVisibility(View.GONE);
                mLlActivityPhoneFriends.setVisibility(View.GONE);
                mTvActivityAddFriendsHint.setText("可能感兴趣的平台");
                mTvShowLine.setVisibility(View.GONE);
                mIvShenHe2.setVisibility(View.VISIBLE);
                mRefreshType = 2;
                doNetwork(mRefreshType);
            }
        });

        mLlActivityQrCode.setOnClickListener(this);

        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                doNetwork(mRefreshType);
            }
        });

        addFriendsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("friendId", groupMemberBeanList.get(position).id);
                ActivityUtils.startActivity(SystemNotifyActivity.this, PersonInfoDetailsActivirt.class, bundle);
            }
        });

        addGroupAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("groupId", groupMemberBeanList.get(position).id);
                ActivityUtils.startActivity(SystemNotifyActivity.this, GroupInfoActivity.class, bundle);
            }
        });

        addPlateformAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Bundle bundle = new Bundle();
//                bundle.putString("friendId", groupMemberBeanList.get(position).id);
//                ActivityUtils.startActivity(AddFriendsAndGroupActivity.this, PersonInfoDetailsActivirt.class);
            }
        });


    }

    private void doNetwork(int type) {
        loadingLayout.showLoading();
        switch (type) {
            case 0://感兴趣的人
                ApiManager.getApi().searchFriendsList("", true, 1, 10).enqueue(new NetCallBack<AddFriendsAndGroupBean>() {
                    @Override
                    protected void onSuccess(Call<AddFriendsAndGroupBean> call, Response<AddFriendsAndGroupBean> response) {
                        if (response != null) {
                            AddFriendsAndGroupBean groupMemberBean = response.body();

                            if (groupMemberBeanList != null) {
                                groupMemberBeanList.clear();

                            }
                            groupMemberBeanList.addAll(groupMemberBean.dataList);
                            mLvActivityListMember.setAdapter(addFriendsAdapter);
                            if (addFriendsAdapter != null) {
                                addFriendsAdapter.notifyDataSetChanged();
                            }

                            if (groupMemberBeanList.size() == 0) {
                                loadingLayout.showEmpty();
                            } else {
                                loadingLayout.showContent();
                            }
                            mRefreshLayout.setRefreshing(false);
                        } else {

                            loadingLayout.showError();
                            mRefreshLayout.setRefreshing(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<AddFriendsAndGroupBean> call, Throwable t) {
                        super.onFailure(call, t);
                        loadingLayout.showError();
                        mRefreshLayout.setRefreshing(false);
                    }
                });
                break;


            case 1://感兴趣的群
                ApiManager.getApi().searchGroupList("", true, 1, 10).enqueue(new NetCallBack<AddFriendsAndGroupBean>() {
                    @Override
                    protected void onSuccess(Call<AddFriendsAndGroupBean> call, Response<AddFriendsAndGroupBean> response) {
                        if (response != null) {
                            AddFriendsAndGroupBean groupMemberBean = response.body();

                            if (groupMemberBeanList != null) {
                                groupMemberBeanList.clear();

                            }
                            groupMemberBeanList.addAll(groupMemberBean.dataList);
                            addGroupAdapter = new AddGroupAdapter(R.layout.list_add_friends_item, groupMemberBeanList);
                            mLvActivityListMember.setAdapter(addGroupAdapter);
                            if (addFriendsAdapter != null) {
                                addFriendsAdapter.notifyDataSetChanged();
                            }

                            if (groupMemberBeanList.size() == 0) {
                                loadingLayout.showEmpty();
                            } else {
                                loadingLayout.showContent();
                            }
                            mRefreshLayout.setRefreshing(false);
                        } else {

                            loadingLayout.showError();
                            mRefreshLayout.setRefreshing(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<AddFriendsAndGroupBean> call, Throwable t) {
                        super.onFailure(call, t);
                        loadingLayout.showError();
                        mRefreshLayout.setRefreshing(false);
                    }
                });
                break;
            case 2://感兴趣的平台
                ApiManager.getApi().searchPaltformList("", true, 1, 10).enqueue(new NetCallBack<AddFriendsAndGroupBean>() {
                    @Override
                    protected void onSuccess(Call<AddFriendsAndGroupBean> call, Response<AddFriendsAndGroupBean> response) {
                        if (response != null) {
                            AddFriendsAndGroupBean groupMemberBean = response.body();

                            if (groupMemberBeanList != null) {
                                groupMemberBeanList.clear();

                            }
                            groupMemberBeanList.addAll(groupMemberBean.dataList);
                            addPlateformAdapter = new AddPlateformAdapter(R.layout.list_add_friends_item, groupMemberBeanList);
                            mLvActivityListMember.setAdapter(addPlateformAdapter);
                            if (addFriendsAdapter != null) {
                                addFriendsAdapter.notifyDataSetChanged();
                            }

                            if (groupMemberBeanList.size() == 0) {
                                loadingLayout.showEmpty();
                            } else {
                                loadingLayout.showContent();
                            }
                            mRefreshLayout.setRefreshing(false);
                        } else {

                            loadingLayout.showError();
                            mRefreshLayout.setRefreshing(false);
                        }
                    }

                    @Override
                    public void onFailure(Call<AddFriendsAndGroupBean> call, Throwable t) {
                        super.onFailure(call, t);
                        loadingLayout.showError();
                        mRefreshLayout.setRefreshing(false);
                    }
                });
                break;
        }

    }

    private void reSelect() {
        mRlUnderway.setSelected(false);
        mRlOver.setSelected(false);
        mRlActivityFlat.setSelected(false);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_activity_qr_code:
                startScanQrcode();
                break;
        }
    }

    public void startScanQrcode() {
        //                假如你要用的是fragment进行界面的跳转
//                IntentIntegrator intentIntegrator = IntentIntegrator.forSupportFragment(ShopFragment.this)
//
        IntentIntegrator intentIntegrator = new IntentIntegrator(SystemNotifyActivity.this);
        intentIntegrator
                .setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)
                .setPrompt("")
                .setOrientationLocked(false)//扫描方向固定
                .setCaptureActivity(QrCodeActivity.class) // 设置自定义的activity是CustomActivity
                .initiateScan(); // 初始化扫描
    }
}
